<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/9
 * Time: 21:16
 */


function pre_dump(&$mix){
    echo '<pre>';
    var_dump($mix);
    echo '</pre>';
}


/*
 * 系统配置的数据处理（dealCurrent）、读取（readConfig）和写入（writeConfig）
 */
function dealCurrent($val){
    if(is_array($val)){
        $rem = $val[1];
        @list($val['title'], $val['mem'], $val['format'], $val['authority']) = explode('|', $rem);
    }
    return $val;
}

function writeConfig($filePath, $cfgArr,$defaultCfg,$lockFile="")
{
    $originalData = readConfig($filePath);
    //local_specialConfig是否存在
    if(!$originalData || $originalData == ""){
        $originalData = $defaultCfg;
        $str = "<?php\n";
        file_put_contents($filePath, $str);
        foreach ($originalData as $k => $v){
            foreach ($cfgArr as $key => $value) {
                if($k == $key){
                    $value = str_replace('\'','',$value);
                    if(strpos($v[0],'\'')===false){
                        $originalData[$k][0] = $value;
                    } else{
                        $originalData[$k][0] = '\''.$value.'\'';
                    }
                }
            }

            if(is_numeric($k)){
                unset($originalData[$k]);
            } else{
                $str = "define('{$k}',{$originalData[$k][0]});\n";
                file_put_contents($filePath, $str, FILE_APPEND);
            }
        }
    } else{
        foreach ($defaultCfg as $k => $v){
            foreach ($originalData as $key => $val){
                if($k == $key){
                    $val[0] = str_replace('\'','',$val[0]);
                    if(strpos($v[0],'\'')===false){
                        $defaultCfg[$k][0] = $val[0];
                    }else{
                        $defaultCfg[$k][0] = '\''.$val[0].'\'';
                    }

                }
            }
        }
        $str = "<?php\n";
        file_put_contents($filePath, $str);
        foreach ($defaultCfg as $k=>$v){
            foreach ($cfgArr as $key=>$val){
                if($k == $key){
                    $val = str_replace('\'','',$val);
                    if(strpos($v[0],'\'')===false) {
                        $defaultCfg[$k][0] = $val;
                    }else{
                        $defaultCfg[$k][0] = '\''.$val.'\'';
                    }
                }
            }
            if(is_numeric($k)){
                unset($defaultCfg[$k]);
            } else{
                $str = "define('{$k}',{$defaultCfg[$k][0]});\n";
                file_put_contents($filePath, $str, FILE_APPEND);
            }
        }
    }
    if($lockFile){
        file_put_contents($lockFile,'使用说明');
    }
}

function readConfig($filePath)
{
    if(!file_exists($filePath)){
        return array();
    }
    $rawContent = file_get_contents($filePath);
    $re = preg_replace('/(\/\*.*\*\/)/s', '', $rawContent);//清除块注释
    $arr = explode("\n", $re);

    $matchArr = array();
    $cfgKv = array();
    foreach ($arr as $key => $one) {
        $r = preg_match('/\w*define\(\s*\'(.+)\'\s*,\s*(\'?[^\']*\'?)\s*\);\s*\/\/(.+)/i', $one, $matchArr);
        if ($r > 0) {
            $cfgKv[$matchArr[1]] = array($matchArr[2], $matchArr[3]);
        } elseif (preg_match('/^\/\/.*$/', $one)) {
            $cfgKv[] = $one;
        } elseif(preg_match('/\w*define\(\s*\'(.+)\'\s*,\s*(\'?[^\']*\'?)\s*\);/i', $one, $matchArr)){
            $cfgKv[$matchArr[1]] = array($matchArr[2]);
        }
    }

    return $cfgKv;
}

//分页模板配对
function fetchPage(&$smart,&$pagerObj){
    $smart->assign('prevPage',$pagerObj->nowPage >1?$pagerObj->nowPage-1:0);
    $smart->assign('nextPage',$pagerObj->nowPage <$pagerObj->totalPages?$pagerObj->nowPage+1:0);
    $smart->assign('nowPage',$pagerObj->nowPage);
    $smart->assign('totalPage',$pagerObj->totalPages);
}




